<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');
$partyId  = isset($_REQUEST['bhatthiPartyId']) ? $_REQUEST['bhatthiPartyId'] : 0;
$openingFinalFineDr = 0;
$openingFinalFineCr = 0;
$openingFinalAmountDr = 0;
$openingFinalAmountCr = 0;
$fromDate      = isset($_REQUEST['bhatthiDate']) ? $_REQUEST['bhatthiDate'] : "";
$toDate        = "";
$weight        = "";
$touch         = "";
$party         = array();
$bhatthiBookCr = array();
$bhatthiBookDr = array();
$opening       = array();
$total         = array();
$bhatthiDebitPrevFine   =  0;
$bhatthiDebitPrevAmount =  0;

if((isset($_REQUEST['party']) && $_REQUEST['party'] > 0) || (isset($_REQUEST['party']) && $_REQUEST['party'] == 0))
{
  $partyId   = $_POST['party'];
  $fromDate  = $_REQUEST['fromDateYear']."-".$_REQUEST['fromDateMonth']."-".$_REQUEST['fromDateDay'];
  $toDate    = $_POST['toDateYear']."-".$_POST['toDateMonth']."-".$_POST['toDateDay'];
  $drCounter = 0;
  $crCounter = 0;

  $total['drWeight']   = 0;
  $total['drTouch']    = 0;
  $total['drFine']     = 0;
  $total['drAmount']   = 0;
  $total['crFine']     = 0;
  $total['crQty']      = 0;
  $total['crAmount']   = 0;

  $opening['crWeight'] = 0;
  $opening['drWeight'] = 0;
  $opening['crTouch']  = 0;
  $opening['drTouch']  = 0;
  $opening['crFine']   = 0;
  $opening['drFine']   = 0;
  $opening['crQty']    = 0;
  $opening['crRate']   = 0;
  $opening['crAmount'] = 0;
  $opening['drAmount'] = 0;



  
  $bhatthiBookDetail = "SELECT bhatthibookdetail.bhatthiBookDetailId, bhatthibook.bhatthiBookId, bhatthibook.bhatthiPartyId, DATE_FORMAT(bhatthibook.bhatthiDate,'%Y-%m-%d') As bhatthiDate,
                                 bhatthibookdetail.particulars,bhatthibookdetail.weight, bhatthibookdetail.touch, bhatthibookdetail.fine,bhatthibookdetail.qty,bhatthibookdetail.grossWeight,bhatthibookdetail.weightPerc,
                                 bhatthibookdetail.rate,bhatthibookdetail.amount, bhatthibookdetail.CrDr, bhatthibook.bhatthiPartyId,bhatthibook.bhatthiPartyId,bhatthibook.prevFine,bhatthibook.prevAmount
                            FROM bhatthibook
                            JOIN bhatthibookdetail ON bhatthibookdetail.bhatthiBookId = bhatthibook.bhatthiBookId
                           WHERE bhatthibookdetail.bhatthiBookId = ".$_GET['bhatthiBookId']."
                             AND bhatthibook.bhatthiPartyId = ".$partyId."
                             AND bhatthibook.bhatthiDate <= '".$toDate."'
                           ORDER BY bhatthibook.bhatthiDate, bhatthibookdetail.bhatthiBookDetailId";
    $bhatthiBookDetailRes = mysql_query($bhatthiBookDetail);
  
  
  while($bhatthiRow = mysql_fetch_assoc($bhatthiBookDetailRes))
  {
     $bhatthiDebitPrevFine   =  $bhatthiRow['prevFine'];
	   $bhatthiDebitPrevAmount =  $bhatthiRow['prevAmount'];
    
    if($bhatthiRow['bhatthiDate'] >= $fromDate)
    {
      if($bhatthiRow['CrDr'] == 'Dr')
      {
        $bhatthiBookDr[$drCounter]['bhatthiBookDetailId'] = $bhatthiRow['bhatthiBookDetailId'];
        $bhatthiBookDr[$drCounter]['bhatthiBookId'] = $bhatthiRow['bhatthiBookId'];
        $bhatthiBookDr[$drCounter]['date']          = $bhatthiRow['bhatthiDate'];
        $bhatthiBookDr[$drCounter]['particulars']   = $bhatthiRow['particulars'];
        $bhatthiBookDr[$drCounter]['weight']       = $bhatthiRow['weight'];
        $bhatthiBookDr[$drCounter]['touch']        = $bhatthiRow['touch'];
        $bhatthiBookDr[$drCounter]['fine']          = $bhatthiRow['fine'];
        $bhatthiBookDr[$drCounter]['qty']          = $bhatthiRow['qty'];
        $bhatthiBookDr[$drCounter]['rate']          = $bhatthiRow['rate'];
        $bhatthiBookDr[$drCounter]['amount']        = $bhatthiRow['amount'];
        $drCounter++;
      }
      else
      {
        $bhatthiBookCr[$crCounter]['bhatthiBookDetailId'] = $bhatthiRow['bhatthiBookDetailId'];
        $bhatthiBookCr[$crCounter]['bhatthiBookId'] = $bhatthiRow['bhatthiBookId'];
        $bhatthiBookCr[$crCounter]['date']          = $bhatthiRow['bhatthiDate'];
        $bhatthiBookCr[$crCounter]['particulars']   = $bhatthiRow['particulars'];
        $bhatthiBookCr[$crCounter]['grossWeight']   = $bhatthiRow['grossWeight'];
        $bhatthiBookCr[$crCounter]['weightPerc']    = $bhatthiRow['weightPerc'];
        $bhatthiBookCr[$crCounter]['weight']        = $bhatthiRow['weight'];
        $bhatthiBookCr[$crCounter]['touch']         = $bhatthiRow['touch'];
        $bhatthiBookCr[$crCounter]['fine']          = $bhatthiRow['fine'];
        $bhatthiBookCr[$crCounter]['qty']           = $bhatthiRow['qty'];
        $bhatthiBookCr[$crCounter]['rate']          = $bhatthiRow['rate'];
        $bhatthiBookCr[$crCounter]['amount']        = $bhatthiRow['amount'];
        if($bhatthiBookCr[$crCounter]['grossWeight'] != 0)
        {
          $bhatthiBookCr[$crCounter]['diffWeight']    = $bhatthiBookCr[$crCounter]['weight'] - $bhatthiBookCr[$crCounter]['grossWeight'];
        }
        else
        {
          $bhatthiBookCr[$crCounter]['diffWeight']    = 0;
        }
        $crCounter++;
      }
    }
    else
    {  // Set opening : Start
      if($bhatthiRow['CrDr'] == 'Dr')
      {
        $opening['drFine']   += $bhatthiRow['fine'];
        $opening['drQty']    += $bhatthiRow['qty'];
        $opening['drAmount'] += $bhatthiRow['amount'];
      }
      else
      {
        $opening['crWeight'] += $bhatthiRow['weight'];
        $opening['crTouch']  += $bhatthiRow['touch'];
        $opening['crFine']   += $bhatthiRow['fine'];
        $opening['crQty']    += $bhatthiRow['qty'];
        $opening['crRate']   += $bhatthiRow['rate'];
        $opening['crAmount'] += $bhatthiRow['amount'];
      }
      $openingFinal['netFineOpening'] = $opening['drFine'] - $opening['crFine'];
	    $openingFinal['netAmountOpening'] = $opening['drAmount'] - $opening['crAmount'];
	    if($openingFinal['netFineOpening'] > 0 )
	    {
	      $openingFinalFineDr = $openingFinal['netFineOpening'];
	      $openingFinalFineCr = "";
	    }
	    else
	    {
	      $openingFinalFineCr = $openingFinal['netFineOpening'];
	      $openingFinalFineDr = "";
	    }
	     if($openingFinal['netAmountOpening'] > 0 )
	    {
	      $openingFinalAmountDr = $openingFinal['netAmountOpening'];
	      $openingFinalAmountCr = "";
	    }
	    else
	    {
	      $openingFinalAmountCr = $openingFinal['netAmountOpening'];
	      $openingFinalAmountDr = "";
	    }
    }// Set opening : End

    // this for total :Start
    if($bhatthiRow['CrDr'] == 'Dr')
    {
      $total['drFine']   += $bhatthiRow['fine'];
      $total['drQty']    += $bhatthiRow['qty'];
      $total['drAmount'] += $bhatthiRow['amount'];
    }
    else
    {
      $total['crFine']   += $bhatthiRow['fine'];
      $total['crQty']   += $bhatthiRow['qty'];
      $total['crAmount'] += $bhatthiRow['amount'];
    }
    $total['totFine']   = ($total['drFine'] - $total['crFine']);
    $total['totAmount'] = ($total['drAmount'] - $total['crAmount']);
    // this for total :End
  }
}



// this for select bhatthi party : Start
$selectBhatthiParty = "SELECT bhatthiPartyId,bhatthipartyName,bhatthiFromDate
                         FROM bhatthiparty
                        ORDER BY bhatthipartyName";
$selectBhatthiPartyRes = mysql_query($selectBhatthiParty);
$a = 0;
while($partyRow = mysql_fetch_assoc($selectBhatthiPartyRes))
{
  $party['id'][$a]              = $partyRow['bhatthiPartyId'];
  $party['name'][$a]            = $partyRow['bhatthipartyName'];
  $party['bhatthiFromDate'][$a] = $partyRow['bhatthiFromDate'];
  $a++;
  if(isset($_POST['bdate']) == "1" && $partyRow['bhatthiPartyId'] == $_POST['party'])
  {
    $fromDate = $partyRow['bhatthiFromDate'];
  }
}
// this for select bhatthi party : End

$smarty = new Smarty;
require("bottom.php");
$smarty->assign("l_partySelect",$l_partySelect);
$smarty->assign("l_from",$l_from);
$smarty->assign("l_to",$l_to);
$smarty->assign("l_find",$l_find);
$smarty->assign("l_date",$l_date);
$smarty->assign("l_particulars",$l_particulars);
$smarty->assign("l_fine",$l_fine);
$smarty->assign("l_amount",$l_amount);
$smarty->assign("l_weight",$l_weight);
$smarty->assign("l_touch",$l_touch);
$smarty->assign("l_qty",$l_qty);
$smarty->assign("l_rate",$l_rate);
$smarty->assign("l_debit",$l_debit);
$smarty->assign("l_credit",$l_credit);
$smarty->assign("l_total",$l_total);
$smarty->assign("l_bhatthiDetail",$l_bhatthiDetail);
$smarty->assign("l_netFine",$l_netFine);
$smarty->assign("l_netAmount",$l_netAmount);
$smarty->assign("l_delete",$l_delete);
$smarty->assign("l_edit",$l_edit);
$smarty->assign("l_save",$l_save);
$smarty->assign("party",$party);
$smarty->assign("bhatthiBookCr",$bhatthiBookCr);
$smarty->assign("bhatthiBookDr",$bhatthiBookDr);
$smarty->assign("bhatthiDebitPrevFine",$bhatthiDebitPrevFine);
$smarty->assign("bhatthiDebitPrevAmount",$bhatthiDebitPrevAmount);
$smarty->assign("opening",$opening);
$smarty->assign("total",$total);
$smarty->assign("partyId",$partyId);
$smarty->assign("fromDate",$fromDate);
$smarty->assign("toDate",$toDate);
$smarty->assign("openingFinalFineDr",$openingFinalFineDr);
$smarty->assign("openingFinalAmountDr",$openingFinalAmountDr);
$smarty->assign("openingFinalFineCr",$openingFinalFineCr);
$smarty->assign("openingFinalAmountCr",$openingFinalAmountCr);
$smarty->assign("doSubmit",(isset($_POST['fromDateDay']) ? 0 : 1));
$smarty->display("bhatthiDetailAavak.tpl");
?>